﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tunnel.EntityClass;

namespace Tunnel
{
    public partial class ManualEventInfoForm : Form
    {
        ManualEventInfo manualEventInfo;

        public enum OperationType
        {
            NewCreate =0,
            Modify=1
        }

        OperationType flag;

        public OperationType Flag
        {
            get
            {
                return flag;
            }

            set
            {
                flag = value;
            }
        }

        public ManualEventInfoForm()
        {
            InitializeComponent();
            Flag = OperationType.NewCreate;
        }
        public ManualEventInfoForm(ManualEventInfo manualEventInfo)
        {
            InitializeComponent();
            this.manualEventInfo = manualEventInfo;
            Flag = OperationType.Modify;

            if (manualEventInfo.StartTime != null)
            {
                startTime.Value = manualEventInfo.StartTime;
            }
            if (manualEventInfo.EndTime != null)
            {
                endTime.Value = manualEventInfo.EndTime;
            }
            tbHappenPlace.Text = manualEventInfo.HappenPlace;
            tbType.Text = manualEventInfo.Type;
            tbInfoSource.Text = manualEventInfo.InfoSource;
            tbDetail.Text = manualEventInfo.Detail;
            tbHandleMethod.Text = manualEventInfo.HandleMethod;
        }
        /// <summary>
        /// 修改手工事件方法
        /// </summary>
        private void ModifyManualEventInfo()
        {
            bwModifyManualEventInfo.RunWorkerAsync();
        }
        /// <summary>
        /// 新建手工事件方法
        /// </summary>
        private void NewCreateManualEventInfo()
        {
            bwNewCreateManualEventInfo.RunWorkerAsync();
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            saveButton.Enabled = false;
            saveButton.Text = "指令下发中";
            switch (Flag)
            {
                case OperationType.NewCreate: NewCreateManualEventInfo(); break;
                case OperationType.Modify: ModifyManualEventInfo(); break;
            }
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            base.DialogResult = DialogResult.Abort;
            this.Close();
        }

        private void bwNewCreateManualEventInfo_DoWork(object sender, DoWorkEventArgs e)
        {
            string sqlNewCreateManualEventInfo = "INSERT INTO [TunnelXAT1].[dbo].[H_ManualEventInfo]([MEIID],[StartTime],[EndTime],[HappenPlace],[Type],[InfoSource],[Detail],[HandleMethod],[Operator],[RegisterTime],[Modifier],[LastModifyTime]) VALUES ('" + new Global().CommandID + "','" + startTime.Text + "','" + endTime.Text + "','" + tbHappenPlace.Text + "','" + tbType.Text + "','" + tbInfoSource.Text + "','" + tbDetail.Text + "','" + tbHandleMethod.Text + "','" + Global.UserID + "',GETDATE(),'',null)";
            SqlDBHelper sdh = new SqlDBHelper();
            sdh.ExecuteCommand(sqlNewCreateManualEventInfo);
        }

        private void bwModifyManualEventInfo_DoWork(object sender, DoWorkEventArgs e)
        {
            string sqlModifyManualEventInfo = "UPDATE [H_ManualEventInfo] SET [StartTime] = '" + startTime.Text + "',[EndTime] = '" + endTime.Text + "',[HappenPlace] = '" + tbHappenPlace.Text + "',[Type] = '" + tbType.Text + "',[InfoSource] = '" + tbInfoSource.Text + "',[Detail] = '" + tbDetail.Text + "',[HandleMethod] = '" + tbHandleMethod.Text + "',[Operator] = '" + manualEventInfo.OperatorUser + "',[RegisterTime] = '" + manualEventInfo.RegisterTime.ToString("yyyy-MM-dd HH:mm:ss") + "',[Modifier] = '" + Global.UserID + "',[LastModifyTime] = GETDATE() WHERE [MEIID] = '" + manualEventInfo.MeID + "'";
            SqlDBHelper sdh = new SqlDBHelper();
            sdh.ExecuteCommand(sqlModifyManualEventInfo);
        }

        private void bwModifyManualEventInfo_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            base.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void bwNewCreateManualEventInfo_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            base.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}
